package com.jxpanda.spring.module.wechat.config.properties;

import com.jxpanda.spring.module.wechat.constant.WechatAppletType;
import com.jxpanda.spring.module.wechat.exception.WechatAppletNotFoundException;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;


/**
 * WechatConfigProperties 类是一个用于配置微信相关属性的记录类。
 * 它通过 @ConfigurationProperties 注解与 Spring Boot 的配置文件绑定，指定前缀为 "panda.spring.wechat"。
 * <p>
 * 目前支持企业微信、小程序、开放平台三种类型的配置信息。
 */
@ConfigurationProperties(prefix = "panda.spring.wechat")
public record WechatConfigProperties(
        @NestedConfigurationProperty
        CorpProperties corp,
        @NestedConfigurationProperty
        MiniProgramProperties miniProgram,
        @NestedConfigurationProperty
        OpenPlatformProperties openPlatform
) {

    private static WechatConfigProperties wechatConfigProperties;

    public static void init(WechatConfigProperties wechatConfigProperties) {
        if (WechatConfigProperties.wechatConfigProperties == null) {
            WechatConfigProperties.wechatConfigProperties = wechatConfigProperties;
        }
    }

    public static WechatConfigProperties wechatConfigProperties() {
        return wechatConfigProperties;
    }

    public AppletProperties getAppletProperties(WechatAppletType appletType, String appName) {
        AppletProperties appletProperties = switch (appletType) {
            case CORP -> corp().applets().get(appName);
            case MINI_PROGRAM -> miniProgram().applets().get(appName);
            case OPEN_PLATFORM -> openPlatform().applets().get(appName);
            default -> null;
        };
        if (appletProperties == null) {
            throw new WechatAppletNotFoundException(appletType, appName);
        }
        return appletProperties;
    }

}
